package problems.tsp;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

import ec.BitSolution;
import ec.FitnessEvaluator;
import ec.Solution;
import ge.ProductionRule;
import problems.Problem;

public class TSPProblem extends Problem {
	private static final int BITS_PER_BLOCK = 8;
	public static int NUM_CITIES = 0;
	public static String DATA_FILE;
	public static String PATH = "src/problems/tsp/";

	public static void main(String[] args) {
		ProductionRule.MAX_DEPTH = 10;
		TSPProblem problem = new TSPProblem();

		int gens = 200;
		int pop = 1000;
		
		String prefix = "gr17";
		problem.DATA_FILE = prefix + ".dat";
		TSPFitnessEvaluator.use_solution_representation = TSPFitnessEvaluator.SOLUTION_REPRESENTATION.AS_PERMUTATOR;
		NUM_CITIES = getNumCities(PATH + DATA_FILE);
		String pth = "data/tsp/gen=" + gens + "_pop=" + pop + "/" + prefix + "/";
		problem.ec(pth, 30, gens, pop, 70, 25, 75, 10);
		problem.randomSearch(pth, 30, gens, pop);
	}

	private static int getNumCities(String data_file) {
		int num_cities = 0;
		
		try {
			BufferedReader file = new BufferedReader(new FileReader(data_file));
			file.readLine(); // MATRIX TYPE
			num_cities = Integer.parseInt(file.readLine().split(":")[1].trim());
			file.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return num_cities;
	}

	@Override
	protected Solution createRandomSolution() {
		return new BitSolution(
				(int) (NUM_CITIES * TSPFitnessEvaluator.use_solution_representation
						.getBlockCountAsCityMultiplier()), BITS_PER_BLOCK, .01);
	}

	@Override
	public FitnessEvaluator getFitnessEvaluator() {
		return new TSPFitnessEvaluator(PATH + DATA_FILE);
	}
}
